9 typedef unsigned int uint32
;
11 #define forsn(i, s, n) for (uint32 i = (s); i < (n); i++)
12 #define forn(i, n) forsn (i, 0, n)
13 #define dforn(i, n) for (uint32 i = (n); i-- > 0;)
16 // Represents a number in 0 <= n < 10 ** 128
18 #define BigInt_NDIGITS 16
19 #define BigInt_BASE 100000000
22 BigInt() : _digits(BigInt_NDIGITS
, 0) {}
24 BigInt(uint32 n
) : _digits(BigInt_NDIGITS
, 0) {
25 assert(n
< BigInt_BASE
);
30 BigInt(const string
& s
) : _digits(BigInt_NDIGITS
, 0) {
35 assert('0' <= s
[i
] && s
[i
] <= '9');
36 digit
+= pow
* (s
[i
] - '0');
39 if (pow
== BigInt_BASE
) {
45 assert(j
< BigInt_NDIGITS
|| digit
== 0);
46 if (j
< BigInt_NDIGITS
) {
51 BigInt
operator+(const BigInt
& n
) {
55 forn (i
, BigInt_NDIGITS
) {
56 const uint32 r
= _digits
[i
] + n
._digits
[i
] + carry
;
57 if (r
>= BigInt_BASE
) {
58 res
._digits
[i
] = r
% BigInt_BASE
;
71 dforn (i
, BigInt_NDIGITS
) {
73 if (_digits
[i
] == 0) continue;
74 printf("%u", _digits
[i
]);
77 printf("%.8u", _digits
[i
]);
86 // Least significant digit first.
87 // Digits are in 0 <= d < BigInt_BASE
88 vector
<uint32
> _digits
;
91 #define print_bigint(x) (x).print()
93 typedef vector
<BigInt
> vBigInt
;
94 typedef vector
<vBigInt
> vvBigInt
;
95 BigInt
distinct_subsequences(const string
& x
, const string
& z
) {
96 vvBigInt
m(2, vBigInt(z
.size() + 1, BigInt(0)));
101 forsn (i
, 1, x
.size() + 1) {
102 forsn (j
, 1, z
.size() + 1) {
103 if (x
[i
- 1] == z
[j
- 1]) {
104 m
[row
][j
] = m
[!row
][j
] + m
[!row
][j
- 1];
106 m
[row
][j
] = m
[!row
][j
];
111 return m
[!row
][z
.size()];
114 int main(int argc
, char **argv
) {
123 print_bigint(distinct_subsequences(x
, z
));